﻿
jQuery(document).ready(function () {
    //getMatLocation('ZZ1000500580040000', function (result) {
    //});

    getLocation();
    
});

function searchProduct() {
    var productMatno = jQuery('#searchProductMatNo').val();
    var productThaiName = jQuery('#searchProductNameThai').val();
    var productEngName = jQuery('#searchProductNameEng').val();
    var productBarcode = jQuery('#searchProductBarcode').val();
    

    if ((productMatno.length > 3 || productThaiName.length > 3) || (productEngName.length > 3 || productBarcode.length > 3)) {

        var Query = "matno=" + productMatno + "&namethai=" + productThaiName + "&nameeng=" + productEngName + "&barcode=" + productBarcode;
        jQuery('#dlgProductSearch').datagrid({
            url: "/api/MaterialApi/PostSearchMaterialWithBarcode?" + Query,
            //loadFilter: pagerFilter,
            //onDblClickRow: function (index, fields, value) {
            //    editNonHR(fields);
            //}
        });
    }
    else {
        alert('กรุณาเลือกเงื่อนไขในการค้นหาสินค้า มากกว่า 4 ตัวอักษร');
    }
}

function openSearchProduct() {
    jQuery('#productSearch').dialog('open').dialog('setTitle', 'ค้นหาสินค้า');
}

function selectProduct() {

    var productSelect = jQuery("#dlgProductSearch").datagrid("getSelected");

    jQuery("#MaterialSearchId").textbox('setValue',productSelect.mmatno);
    //jQuery("#productName").val(productSelect.mmatthai);
    jQuery('#productSearch').dialog('close')
    MaterialSearch();
}


function MaterialSearch() {

    var matId = jQuery("#MaterialSearchId").val();
    if (matId != undefined && matId != "") {
        getMatLocation(matId, function (result) {
            BindingMatSearch(result.MatDetails, result.MatDetailsExtentsion,result.MatLocations);
        });
    }
}

function clearForm() {
    $('#materialform').form('clear');
    $('#MaterialSearchId').textbox('clear');
}

function BindingMatSearch(MatDetails, MatDetailsExtension, MatLocations) {
    $('#materialform').form('clear');
    jQuery('#materialform').form('load',MatDetails);
    jQuery('#materialform').form('load', MatDetailsExtension);
    var allBranchs = jQuery('#branchAll').datagrid('getRows');

    jQuery('#branchAll').datagrid('uncheckAll');
    jQuery.each(MatLocations, function (i,d) {
        jQuery.each(allBranchs, function (i2, d2) {
            if (jQuery.trim(d.LocationNo) == jQuery.trim(d2.mbranchno)){
                jQuery('#branchAll').datagrid('checkRow', i2);
            }
        });
    });
}

function Save(isdraft) {
    
    var mmatnoValue = jQuery('#mmatno').val();
    var sizeunitValue = jQuery('#sizeunit').val();
    var setunitvalue = jQuery('#setunit').val();
    var quantitypersetunitValue = jQuery('#quantitypersetunit').val();
    var packageunitValue = jQuery('#packageunit').val();
    var setunitsizeValue = jQuery('#setunitsize').val();


    var allbranchs = $('#branchAll').datagrid('getSelections');
    var matlocationsave = [];
    jQuery.each(allbranchs, function (i,d) {
        matlocationsave.push({ MatNo: mmatnoValue, LocationNo: d.mbranchno, IsEnable: true });
    });

    var matsave = {
        mmatno: mmatnoValue,
        sizeunit: sizeunitValue,
        setunit: setunitvalue,
        quantitypersetunit: quantitypersetunitValue,
        packageunit: packageunitValue,
        setunitsize: setunitsizeValue
    };

    jQuery.ajax({
        type: "POST",
        url: "/api/MaterialApi/PostSaveMat",
        data: JSON.stringify({ matDetail: matsave, matLocation: matlocationsave }),
        contentType: "application/json",
        success: function (result) {
            if (result == true) {
                alert('Save Success');
            }
        }

    });

    //matsave["mmatno" = ""];
}

function getMatLocation(Matno, func) {
    jQuery.ajax({
        type: "GET",
        url: "/api/MaterialApi/GetMatByMatNo",
        data: { matNo: Matno },
        success: function (result) {
            func(result);
        }
        
    });
}

function getLocation(func) {
    jQuery.ajax({
        type: "GET",
        url: "/api/BranchApi/GetAllActiveBranch",
        success: function (result) {
            jQuery('#branchAll').datagrid({
                data: result.AllBranch
            });
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
        
    });
}